package com.gxa.sso.repository;

import com.gxa.sso.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.RepositoryDefinition;
import org.springframework.data.repository.query.Param;

import java.util.List;
//CrudRepository 包含基本的增删改查操作

/**
 * jpa 持久化操作针对单表
 */
//@RepositoryDefinition(domainClass = User.class,idClass = Long.class)
public interface UserRepository extends CrudRepository<User,Long> {

    /**
     * 通过name查询目标User的方法
     * 转换为sql  where user_phone=?
     */
    User findByPhone(String phone);

    /**
     * 通过name查询目标User的方法
     * 转换为sql  where user_name=?
     */
    List<User> findByName(String name);

    /**
     *  where user_name like '%?%'
     */
    List<User> findByNameContains(String name);

    /**
     *  where user_id in(?,?,?)
     */
  /*  List<User> findByIdIn(Long... ids);

    List<User> findByIdIn2(List<Long> ids);*/

    /**
     *  where user_phone=? AND user_password=?
     */
    User findByPhoneAndPassword(String phone,String password);

    /**
     * where user_name like '%?%' limit ?,?
     * @param name
     * @return
     */
    Page<User> findAllByNameContaining(String name, Pageable pageable);

    /**
     *  HQL: Hibernate Query Language 特点是面向对象的SQL
     *      select * from tb_user where user_phone=?
     * @param phone 手机号
     * @param password 密码
     * @return
     */
    @Query("select user from User user where phone=?1 and password=?2")
    User selectDemo1(@Param("phone") String phone,@Param("password") String password);

    @Query("select user from User user where phone=:phone and password=:password")
    User selectDemo2(@Param("phone") String phone,@Param("password") String password);


    /**
     *  JPA还可以执行原生的SQL
     * @return
     */
    @Query(nativeQuery = true,value = "select * from tb_user where user_id=?")
    List<User> selectDemo3(Long id);

}
